<script setup>
import {
  listRefers,
  listDept,
  delRefer,
  addRefer,
  getReferInfo,
  updateRefer,
} from "@/api/hospital";
import { computed } from "vue";
const { proxy } = getCurrentInstance();
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const loading = ref(true);
const list = ref([]);
const total = ref(0);
const open = ref(false);
const title = ref("");
const deptList = ref([]);
// 查询参数
const data = reactive({
  form: {},
  rules: {},
});

const { form, rules } = toRefs(data);

function resetForm() {
  form.value = {
    refer_id: undefined,
    out_dept: userStore.deptId,
    in_dept: undefined,
    doctor: userStore.id,
    doctorName: userStore.nick,
    create_date: new Date(),
    patient_id: undefined,
    diagnosis: "-",
    according: "-",
  };
  proxy.resetForm("referRef");
}

function handleAdd() {
  resetForm();
  title.value = "新建转介申请单";
  open.value = true;
}

function handleDelete(row) {
  const status = row.status;
  if (status == 1) {
    proxy.$modal.msgError("已锁定的转介单不能删除");
    return;
  }
  const rIds = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除转介单编号为"' + rIds + '"的数据项？')
    .then(function () {
      return delRefer(rIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

function handleUpdate(row) {
  const status = row.status;
  if (status == 1) {
    proxy.$modal.msgError("已锁定的转介单不能删除");
    return;
  }
  resetForm();
  getReferInfo(row.id).then((res) => {
    if (res.code == 200) {
      form.value = res.data;
      form.value.doctorName = userStore.nick;
      title.value = "修改转介申请单";
      open.value = true;
    } else {
      proxy.$modal.msgError(res.msg);
    }
  });
}
function getList() {
  loading.value = true;
  listRefers().then((res) => {
    loading.value = false;
    list.value = res.data;
    total.value = res.total;
  });
}

/** 获取部门列表 */
function getDeptList() {
  listDept().then((res) => {
    deptList.value = res.data.slice(1); // 排除信息科
  });
}

function submitForm() {
  open.value = false;
  proxy.$refs["referRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateRefer(form.value).then(() => {
          getList();
          proxy.$modal.msgSuccess("修改成功");
        });
      } else {
        addRefer(form.value).then((res) => {
          if (res.code == 200) {
            getList();
            proxy.$modal.msgSuccess("新增成功");
          } else {
            proxy.$modal.msgError(res.msg);
          }
        });
      }
    } else {
      console.log("校验失败");
      return false;
    }
  });
}

function cancel() {
  open.value = false;
}

getDeptList();
getList();
</script>
<template>
  <div class="app-container">
    <h1>转介申请单</h1>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column
        label="转介单编号"
        align="center"
        key="id"
        prop="id"
        width="90"
      />
      <el-table-column
        label="患者编号"
        align="center"
        key="patientId"
        prop="patientId"
        width="100"
      />
      <el-table-column
        label="申请时间"
        align="center"
        key="createDate"
        prop="createDate"
      />
      <el-table-column
        label="转出科室名称"
        align="center"
        key="outDoctName"
        prop="outDoctName"
      />
      <el-table-column
        label="转入科室名称"
        align="center"
        key="inDoctName"
        prop="inDoctName"
      />
      <el-table-column
        label="介绍医生"
        align="center"
        key="doctorName"
        prop="doctorName"
      />
      <el-table-column label="状态" align="center" key="status">
        <template #default="scope">
          <el-tag :type="scope.row.status == 0 ? 'success' : 'info'">
            {{ scope.row.status == 0 ? "可修改" : "已锁定" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改转介申请单配置对话框 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form :model="form" :rules="rules" ref="referRef" label-width="80px">
        <el-row>
          <el-form-item label="患者编号" prop="patient_id">
            <el-input v-model="form.patient_id" />
          </el-form-item>
          <el-form-item label="转出科室" prop="out_dept">
            <el-select
              filterable
              v-model="form.out_dept"
              placeholder="请选择转出科室"
            >
              <el-option
                v-for="dict in deptList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="介绍医生" prop="doctorName">
            <el-input v-model="form.doctorName" disabled />
          </el-form-item>
          <el-form-item label="转入科室" prop="in_dept">
            <el-select
              filterable
              v-model="form.in_dept"
              placeholder="请选择转入科室"
            >
              <el-option
                v-for="dict in deptList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="转介时间" prop="create_date">
            <el-date-picker
              v-model="form.create_date"
              type="date"
              placeholder="请输入转介时间"
            />
          </el-form-item>
          <el-col :span="24">
            <el-form-item label="医疗诊断" prop="diagnosis">
              <el-input
                v-model="form.diagnosis"
                placeholder="请输入医疗诊断"
                type="textarea"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="转介依据" prop="according">
              <el-input
                v-model="form.according"
                placeholder="请输入转介依据"
                type="textarea"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
</style>
